package cn.fuxi2022.module.info.service.impl;

import cn.fuxi2022.commons.redis.service.CaptchaService;
import cn.fuxi2022.core.exception.SmartGroupException;
import cn.fuxi2022.core.utils.PageUtils;
import cn.fuxi2022.core.utils.Query;
import cn.fuxi2022.core.utils.SmsUtils;
import cn.fuxi2022.model.empty.applets.GroupStudentInfo;
import cn.fuxi2022.module.info.mapper.GroupStudentInfoMapper;
import cn.fuxi2022.module.info.service.GroupStudentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author admin
 * @description 针对表【group_student_info(社员详细信息表)】的数据库操作Service实现
 * @createDate 2022-09-24 20:46:12
 */
@Service
public class GroupStudentInfoServiceImpl extends ServiceImpl<GroupStudentInfoMapper, GroupStudentInfo>
        implements GroupStudentInfoService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<GroupStudentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(GroupStudentInfo::getGmtModified);

        // 如果 is_student 不为空，将判断当前的阶段，系统将返回当前阶段及其阶段后的学生信息
        if (ObjectUtils.isNotEmpty(params.get("is_student"))) {
            boolean flag = false;
            int isStudent = Integer.parseInt((String) params.get("is_student"));
            if (isStudent <= 3) {
                wrapper.eq(GroupStudentInfo::getIsStudent, "3");
                flag = true;
            }
            if (isStudent <= 2) {
                if (flag) {
                    wrapper.or();
                }
                wrapper.eq(GroupStudentInfo::getIsStudent, "2");
                flag = true;
            }
            if (isStudent <= 1) {
                if (flag) {
                    wrapper.or();
                }
                wrapper.eq(GroupStudentInfo::getIsStudent, "1");
                flag = true;
            }
            if (isStudent <= 0) {
                if (flag) {
                    wrapper.or();
                }
                wrapper.eq(GroupStudentInfo::getIsStudent, "0");
                flag = true;
            }
        }

        // 查询支付 与 未支付的学生
        if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(params.get("pay_status"))) {
            int status = Integer.parseInt((String) params.get("pay_status"));
            // 0未支付 1已支付
            if (status == 0) {
                // 查询正在支付状态的社员
                wrapper.eq(GroupStudentInfo::getIsStudent, 1);
            } else if (status == 1) {
                // 查询正在入群和完成的社员
                wrapper.eq(GroupStudentInfo::getIsStudent, 2).or().eq(GroupStudentInfo::getIsStudent, 3);
            }
        }

        IPage<GroupStudentInfo> page = this.page(
                new Query<GroupStudentInfo>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Autowired
    private CaptchaService captchaService;

    @Autowired
    private SmsUtils smsUtils;

    @Override
    public boolean getCaptcha(String phone) {
        // 不做次数判断
        String captcha;
        // 1.将验证码传入redis
        if (!(StringUtils.hasText(captcha = captchaService.saveCaptcha(phone)))) {
            throw new SmartGroupException("你已经获取过验证码了，稍后再试吧");
        }

        // 2.使用短信平台发送验证码短信
        if (!smsUtils.sendCaptcha(phone, captcha, "1")) {
            throw new SmartGroupException("验证码发送失败，请检查手机号");
        }

        return true;
    }
}




